package com.goldplusgold.tradedelay.app.application;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;


import com.squareup.leakcanary.LeakCanary;

import java.util.List;


/**
 * Created by admin on 2017/5/2.
 */

public class TDApplication extends Application {

    private static TDApplication application;
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        LeakCanary.install(this);
        String processName = getProcessName(this, android.os.Process.myPid());
        if (processName != null) {
            boolean defaultProcess = processName.equals("com.goldplusgold.tradedelay.app") || processName.equals("com.goldplusgold.tradedelay.app.debug");
            if (defaultProcess) {
                TDInitApplication proxy = new TDInitApplication(this);
                proxy.init();
            }
        }
/*
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);*/
        // Normal app init code...

    }

    public static TDApplication getApplication(){
        return application;
    }

    /**
     * @return null may be returned if the specified process not found
     */
    public String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName;
            }
        }
        return null;
    }


}
